package com.cooper.mediadb;

import com.cooper.scrapemedialib.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class SearchActionBarLayout extends LinearLayout {

	public interface OnSearchBackListener {
		public abstract void onSearchBack();

		public abstract void onSearch(String keyword);
	}

	// private static Activity mSearchActivity;;

	private View actionLayout;
	private View searchActionLayout;
	private OnSearchBackListener onSearchBackListener;
	private View searchEditView;

	public SearchActionBarLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public SearchActionBarLayout(Context context) {
		super(context);
	}

	public void setOnSearchBackListener(
			OnSearchBackListener onSearchBackListener) {
		this.onSearchBackListener = onSearchBackListener;
	}

	private void searchBack() {
		searchEditView.clearFocus();
		InputMethodManager imm = (InputMethodManager) getContext()
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm.isActive(searchEditView))
			imm.hideSoftInputFromWindow(searchEditView.getWindowToken(), 0);

		actionLayout.setVisibility(View.VISIBLE);
		searchActionLayout.setVisibility(View.GONE);

		if (onSearchBackListener != null) {
			onSearchBackListener.onSearchBack();
		}
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		searchEditView = findViewById(R.id.search_edit_text);

		if (searchEditView != null) {

			((EditText) searchEditView)
					.setOnEditorActionListener(new OnEditorActionListener() {

						@Override
						public boolean onEditorAction(TextView v, int actionId,
								KeyEvent event) {

							if (actionId == EditorInfo.IME_ACTION_SEARCH) {
								if (onSearchBackListener != null) {
									onSearchBackListener.onSearch(v.getText()
											.toString());
								}
							}
							return false;
						}

					});
		}

		actionLayout = findViewById(R.id.action_layout);
		searchActionLayout = findViewById(R.id.search_action_layout);
		View buttonView = findViewById(R.id.search_action_button);
		if (buttonView != null) {
			((ImageButton) buttonView)
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							actionLayout.setVisibility(View.GONE);
							searchActionLayout.setVisibility(View.VISIBLE);
							searchEditView.requestFocus();

							InputMethodManager imm = (InputMethodManager) getContext()
									.getSystemService(
											Context.INPUT_METHOD_SERVICE);
							imm.showSoftInput(searchEditView, 0);
						}
					});
		}
		buttonView = findViewById(R.id.nav_back_button);
		if (buttonView != null) {
			((ImageButton) buttonView)
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {

							searchBack();
						}
					});
		}
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
				&& event.getAction() == KeyEvent.ACTION_UP
				&& actionLayout.getVisibility() != View.VISIBLE) {
			searchBack();
			return true;

		}
		return super.dispatchKeyEvent(event);
	}
	// public static void setSearchActivity(Activity searchActivity) {
	// mSearchActivity = searchActivity;
	// }

	/**
	 * Overrides the handling of the back key to move back to the previous
	 * sources or dismiss the search dialog, instead of dismissing the input
	 * method.
	 */
	// @Override
	// public boolean dispatchKeyEventPreIme(KeyEvent event) {
	//
	// //this.dispatchKey
	// Log.d(TAG, "dispatchKeyEventPreIme(" + event + ")");
	// if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
	// KeyEvent.DispatcherState state = getKeyDispatcherState();
	// if (state != null) {
	// if (event.getAction() == KeyEvent.ACTION_DOWN
	// && event.getRepeatCount() == 0) {
	// state.startTracking(event, this);
	// return true;
	// } else if (event.getAction() == KeyEvent.ACTION_UP
	// && !event.isCanceled() && state.isTracking(event)) {
	// searchEditView.clearFocus();
	//
	// return true;
	// }
	// }
	// }
	//
	// return super.dispatchKeyEventPreIme(event);
	// }
}
